package com.example.homework9;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.Log;

public class DipUtils {
	private float density;
	
	private static DipUtils utils;
	
	public static DipUtils getInstance() {
		if (utils == null) {
			utils = new DipUtils();
		}
		
		return utils;
	}
	
	public void init(Activity activity) {
		DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
		this.density = displayMetrics.density;
		int dpi = displayMetrics.densityDpi;
		Log.d("TEST", String.format("density: %f, dpi: %d", this.density, dpi));
	}
	
	public int DpToPixel(float dp) {
		return (int) (dp * this.density);
	}
	
	public float PixcelToDp(int pixel) {
		return pixel/this.density;
	}
	
	public int fromExtraHighDiensityPixel(int pixel) {
		return (int) (pixel / (320 / 160.0f) * this.density);
	}
	
	public int fromHighDiensityPixel(int pixel) {
		return (int) (pixel / (240 / 160.0f) * this.density);
	}
	
	public int fromMediumDiensityPixel(int pixel) {
		return (int) (pixel / (160 / 160.0f) * this.density);
	}
	
	public int fromLowDiensityPixel(int pixel) {
		return (int) (pixel / (120 / 160.0f) * this.density);
	}
}
